package com.qiahao.service.impl;

import com.github.pagehelper.StringUtil;
import com.qiahao.dto.PageDTO;
import com.qiahao.bean.Phone;
import com.qiahao.mapper.PhoneMapper;
import com.qiahao.service.ImportExcelService;
import com.qiahao.util.ExcelUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class ImportExcelServiceImpl implements ImportExcelService {

    @Autowired
    PhoneMapper phoneMapper;


    @Override
    public PageDTO<Phone> importExcel(MultipartFile file) {
        PageDTO<Phone> data = new PageDTO<>();
        List<Phone> importData = new ArrayList<>();
        String error = "";

        if (file.isEmpty()) {
            error = "文件不存在";

        }
        List<List<Object>> listob = null;
        InputStream in = null;
        try {
            in = file.getInputStream();

            try {
                listob = ExcelUtils.getBankListByExcel(in, file.getOriginalFilename());
            } catch (Exception e) {
                e.printStackTrace();
                error = "导入失败,服务器异常";
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        data.setMsg(StringUtil.isEmpty(error) ? "导入成功" : error);
        data.setData(importData);

        for (int i = 0; i < listob.size(); i++) {
            List<Object> ob = listob.get(i);
            //数据格式不正确直接过滤
            if (ob == null || ob.size() != 2) {
                continue;
            }

            //过滤重复号码
            String phoneNum = ob.get(0).toString();
            if (phoneMapper.findPhoneById(phoneNum) != null) {
                continue;
            }
            Phone phone = new Phone();
            phone.setPhone(phoneNum);
            phone.setUserName(ob.get(1).toString());
            phoneMapper.AddPhone(phone);
//            for (int j=0;j<ob.size();j++){
//
//                Phone phone = new Phone();
//                phone.setPhone(ob.get(j)+"");
//                importData.add(phone);
//            }

        }
        data.setTotalCount((long) importData.size());
        data.setPageNo(1);
        data.setTotalPage((importData.size() + 20 - 1) / 20);
        return data;
    }

    @Override
    public PageDTO<Phone> getPhoneList(int pageNo, int pageSize) {

        PageDTO<Phone> dto = new PageDTO();
        Page<Phone> page = PageHelper.startPage(pageNo, pageSize);
        dto.setData(phoneMapper.getAllPhone());
        dto.setTotalCount(page.getTotal());
        dto.setPageNo(pageNo);
        dto.setTotalPage(page.getPages());
        return dto;
    }

    @Override
    public Map<String, Object> addPhone(Phone phone) {
        Map<String, Object> map = new HashMap<>();
        phoneMapper.AddPhone(phone);
        map.put("msg", "保存成功");
        return map;
    }
}
